<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class CouponCategory extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'coupon_category';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'online_text'
    ];
    

    protected static function init()
    {
//        self::afterInsert(function ($row) {
//            $pk = $row->getPk();
//            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
//        });
    }

    
    public function getOnlineList()
    {
        return ['0' => __('Online 0'), '1' => __('Online 1')];
    }


    public function getOnlineTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['online']) ? $data['online'] : '');
        $list = $this->getOnlineList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public static function getCategories()
    {
        return self::where('online', 1)->order('weigh desc')->select();
    }

}
